/**
 * Created with JetBrains WebStorm.
 * User: Ajay
 * Date: 10/11/13
 * Time: 4:40 PM
 * To change this template use File | Settings | File Templates.
 */

function showRightMenu(){
    var userFirstName = localStorage.getItem('userFirstName');
    $('#username').text("Logged in as "+userFirstName);
    $('#userrightmenu').show();
    }

function doLogout(){
    localStorage.setItem("login","false");
    window.location.href = "/";
    }

var $container = $('#wishcontainer');
// initialize

$(document).ready(function() {

    CategoryModel = Backbone.Model.extend({
        defaults: {
            url: '',
            type: '',
            name: ''
        }
});

var wishListDataurl = 'http://localhost:8080/soshal/api/1.0/user/' + currSessionID + '/wishlist';

var chooseRightTemplate = $('#wishelement_template_me').html();
if (wishListRequiredFor !== 'me')
            {
                wishListDataurl = wishListDataurl + '/' + wishListRequiredFor;
                chooseRightTemplate = $('#wishelement_template_other').html()
                }

WishListModalCollection = Backbone.Collection.extend({
    url: wishListDataurl,
    category: CategoryModel,
    user: "",
    status: "",
    noOfUsersWished:""
    });

WishElementView = Backbone.View.extend({
    template: _.template(chooseRightTemplate),
    el: $("#wishcontainer"),
    initialize: function() {
    _.bindAll(this, 'addOne', 'addAll', 'render'); // bind 'this' in 'render'
    this.collection = new WishListModalCollection();
    this.collection.bind('change', this.render);
    var self = this;
    this.collection.fetch({
    success: function() {
    $('#loading').hide();
    self.render();
    $container.masonry({
    columnWidth: 240,
    itemSelector: '.wishelement'
    });

$(".iwishtoo").click(function() {
    var outerdiv = $(this).parent('.wishelement');
    saveWish("save", outerdiv);
    });

$(".alreadybought").click(function() {
    var outerdiv = $(this).parent('.wishelement');
    saveWish ("bought", outerdiv);
    });

}
});
},
render: function() {
    $(this.el).html(this.template({results: this.collection.models}));
}
});

var myModelView = new WishElementView();
//jQuery("img.lazy").lazy();

});

function saveWish(status, wishelement)
{
   $('#popup').bPopup();
}